package com.ycfmedical.bed.adapter;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.ycfmedical.bed.R;
import com.ycfmedical.bed.bean.Videoinfo;
import com.zhy.autolayout.utils.AutoUtils;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.base.SimpleRecAdapter;
import cn.droidlover.xdroidmvp.imageloader.ILFactory;
import cn.droidlover.xdroidmvp.kit.KnifeKit;
import cn.droidlover.xdroidmvp.log.XLog;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

/**
 * Created by Egoist on 2017-04-20.
 */

public class VideoPlayAdapter extends SimpleRecAdapter<Videoinfo, VideoPlayAdapter.ViewHolder>
{
    private static final String TAG = "VideoPlayAdapter";
    public VideoPlayAdapter(Context context)
    {
        super(context);
    }
    
    @Override
    public ViewHolder newViewHolder(View itemView)
    {
        return new ViewHolder(itemView);
    }
    
    @Override
    public int getLayoutId()
    {
        return R.layout.layout_item_videoinfo;
    }
    
    @Override
    public void onBindViewHolder(ViewHolder holder, int position)
    {
          final Videoinfo item = data.get(position);
          XLog.d(TAG, "onBindViewHolder [" + holder.itemVideoplayer.hashCode() + "] position=" + position);

          JCVideoPlayer.NORMAL_ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        if(item.getVideoName().equals(getString(R.string.video_item_trans)))
        {
            holder.itemVideoplayer.widthRatio = 21;
            holder.itemVideoplayer.heightRatio = 10;
        }
        else
        {
            holder.itemVideoplayer.widthRatio = 23;
            holder.itemVideoplayer.heightRatio = 10;
        }

          holder.itemVideoplayer.setUp(item.getVideoUrl(), JCVideoPlayer.SCREEN_LAYOUT_LIST,
                  item.getVideoName());
          holder.itemBtn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  holder.itemVideoplayer.onClick(holder.itemVideoplayer.fullscreenButton);
                  XLog.d("holder.itemBtn onClick(holder.itemVideoplayer.fullscreenButton)");
              }
          });
          ILFactory.getLoader().loadResource(holder.itemVideoplayer.thumbImageView,item.getVideoThumbs(),null);

    }
    
    public static class ViewHolder extends RecyclerView.ViewHolder
    {
        @BindView(R.id.item_videoplayer)
        JCVideoPlayerStandard itemVideoplayer;
        @BindView(R.id.full_btn)
        Button itemBtn;
        public ViewHolder(View itemView)
        {
            super(itemView);
            KnifeKit.bind(this, itemView);
            AutoUtils.autoSize(itemView);
        }
    }
    
    public static class SpaceItemDecoration extends RecyclerView.ItemDecoration
    {
        
        private int space;
        
        public SpaceItemDecoration(int space)
        {
            this.space = space;
        }
        
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state)
        {
            
            if (parent.getChildPosition(view) != 0)
                outRect.top = space;
            
        }
    }
}
